package com.common.db;

import java.sql.SQLException;

import javax.sql.DataSource;

import org.apache.commons.dbutils.QueryRunner;

import com.alibaba.druid.pool.DruidPooledConnection;

public class DBDruidUtils {
	private DataSource ds = null;
	private QueryRunner runner = null;
	private DruidPooledConnection druidPooledConnection = null;

	public DBDruidUtils() {
		try {
			this.ds = DbPoolConnection.getInstance().getDataSource();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		if (this.ds != null) {
			this.runner = new QueryRunner(this.ds);
		}
	}

	public DBDruidUtils(String corp, String dbString) {
		try {
			// 会议
			if (corp.equals("meet")) {
				DbPoolConnection_meet.dbString = dbString;
				this.ds = DbPoolConnection_meet.getInstance().getDataSource();
			}
			// 一卡通
			else if (corp.equals("onecard")) {
				DbPoolConnection_onecard.dbString = dbString;
				this.ds = DbPoolConnection_onecard.getInstance().getDataSource();
			}
			// 一卡通
			else if (corp.equals("onecardSP")) {
				DbPoolConnection_onecard.dbString = dbString;
				this.ds = DbPoolConnection_onecardSP.getInstance().getDataSource();
			}
			// 一卡通
			else if (corp.equals("onecardHB")) {
				DbPoolConnection_onecard.dbString = dbString;
				this.ds = DbPoolConnection_onecardHB.getInstance().getDataSource();
			}
			// 一卡通
			else if (corp.equals("onecardHK")) {
				DbPoolConnection_onecard.dbString = dbString;
				this.ds = DbPoolConnection_onecardHK.getInstance().getDataSource();
			}
			// lims
			else if (corp.equals("lims")) {
				DbPoolConnection_lims.dbString = dbString;
				this.ds = DbPoolConnection_lims.getInstance().getDataSource();
			} // tm
			else if (corp.equals("tm")) {
				DbPoolConnection_tm.dbString = dbString;
				this.ds = DbPoolConnection_tm.getInstance().getDataSource();
			} // mes
			else if (corp.equals("mes")) {
				DbPoolConnection_tm.dbString = dbString;
				this.ds = DbPoolConnection_mes.getInstance().getDataSource();
			} else {
				DbPoolConnection_meet.dbString = dbString;
				this.ds = DbPoolConnection.getInstance().getDataSource();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		if (this.ds != null) {
			this.runner = new QueryRunner(this.ds);
		}
	}

	public DBDruidUtils(DataSource ds) {
		this.ds = ds;
		this.runner = new QueryRunner(this.ds);
	}

	public QueryRunner getRunner() {
		return this.runner;
	}

	public DruidPooledConnection getConnection() throws SQLException {
		this.druidPooledConnection = DbPoolConnection.getConnection();
		return this.druidPooledConnection;
	}
}
